﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Zerg
{
	public class ActionCallSpawnLarvae : Action
	{
		public static ActionCallSpawnLarvae Instance = new ActionCallSpawnLarvae();
		private ActionCallSpawnLarvae() { }
		public override bool HaveResources(IBuildOrder bo)
		{
			ZergState state = bo.State as ZergState;
			for (int c = 0; c < state.Queen_Energy.Count; c++)
				if (state.Queen_Energy[c] >= 50)
					return true;
			return false;
		}

		public override bool HaveTechnology(IBuildOrder bo)
		{
			ZergState state = bo.State as ZergState;
			return state.Queen > 0;
		}

		public override bool IsInvalid(IBuildOrder bo)
		{
			ZergState state = bo.State as ZergState;
			return state.Queen + state.Queen_Building == 0;
		}

		public override void Execute(IBuildOrder bo)
		{
			ZergState state = bo.State as ZergState;
			for (int c = 0; c < state.Queen_Energy.Count; c++)
				if (state.Queen_Energy[c] >= 50)
				{
					state.Queen_Energy[c] -= 50;
					break;
				}
			///TODO add base selection cause normal larva production does not take back 
			///until they are less than 3 larva (for one base), 
			///the problem is that here we dont know which larva belong to which base
			bo.AddAction(state.Time + 40, new ActionGeneric("ActionSpawnedSpawnLarvae", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
			{
				ZergState ibostate = ibo.State as ZergState;
				ibostate.Larva+=4;
			}));
		}

		private IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceQueen.Instance } };
				return m_Requirements;
			}
		}

	}
}
